package com.rxh.util;


import java.io.OutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsxView;

/**
 * 
 * @author xuzm
 * @date 20180411
 *
 */
public class ExcelExportByPoiUtils extends AbstractXlsxView implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String filename = null;
		Object reconDate = request.getAttribute("reconDate");
		Integer reconType = (Integer) request.getAttribute("reconType");
		if(reconType == 1){
			
			filename = "businessRecon_" + reconDate + ".xlsx";

		}else{
			
			SimpleDateFormat reconDateFmt = new SimpleDateFormat("yyyy-MM");
			reconDate = reconDateFmt.format(reconDate);
			filename = "businessMonthRecon_" + reconDate + ".xlsx";
			
		}
    	response.reset();
        response.setContentType("application/vnd.ms-excel");     
        response.setHeader("Content-disposition", "attachment;filename=" + filename);     
        OutputStream ouputStream = response.getOutputStream();   
        workbook.write(ouputStream);
        ouputStream.flush();     
        ouputStream.close(); 
		
	}  
	
	
}
